package com.ljh.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

/**
 * @author: Themberfue
 * @date: 2025/4/25 18:53
 * @description:
 */
@Slf4j
public class MailUtil {
    @Value(value = "${spring.mail.username}")
    private String from;

    private final JavaMailSender mailSender;

    public MailUtil(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    /**
     * 发邮件
     * @param to: 目标邮箱地址
     * @param subject：标题
     * @param context：正文
     */
    public void sendSampleMail(String to, String subject, String context) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(context);
        try {
            mailSender.send(message);
        } catch (Exception e) {
            log.error("向 {} 发送邮件失败！", to, e);
        }
    }
}
